package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.CheckGroupDto;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupItem;
import com.itheima.service.CheckGroupItemService;
import com.itheima.service.CheckGroupService;
import com.itheima.vo.CheckGroupVO;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckGroupServiceimpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Reference
    CheckGroupService checkGroupService;

    @Autowired
    CheckGroupItemService checkGroupItemService;

    @Override
    @Transactional
    public boolean add(CheckGroupDto checkGroupDTO) {
        //保存检查组
        this.saveOrUpdate(checkGroupDTO.getCheckGroup());
        //先删除关系表数据
        checkGroupItemService.deleteByGroupId(checkGroupDTO.getCheckGroup().getId());
        //再新增关系表数据
        //3.维护关系数据

        System.out.println("item------------------>");
        List<CheckGroupItem> items = new ArrayList<>();
        for (Long itemId : checkGroupDTO.getItemIds()) {
            CheckGroupItem checkGroupItem = new CheckGroupItem();
            checkGroupItem.setItemId(itemId);
            checkGroupItem.setGroupId(checkGroupDTO.getCheckGroup().getId());
            items.add(checkGroupItem);
//            checkGroupItemService.save(checkGroupItem);
        }
        //一次存储多个数据
        System.out.println(items.size()+"----->");
        checkGroupItemService.saveBatch(items);
        return true;
    }

    @Override
    public List<CheckGroup> findall() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("delete_flag", 0);
        List list = this.list(wrapper);
        return list;
    }

    @Override
    public void deleteByGroupId(Long id) {
        //删除组
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        this.remove(queryWrapper);
        //删除组关系
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("group_id",id);
        checkGroupItemService.remove(queryWrapper1);
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        //queryWrapper后调用需要搞的方法
        //下面俩必须加上泛型，不然consumer会报错
        IPage<CheckGroup> page = new Page(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //加上泛型，不然下面识别不了
        System.out.println(page.getCurrent());
        System.out.println(page.getSize());
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper();
        queryWrapper.eq("delete_flag", 0);
        if (queryPageBean.getQueryString() != null && "".equals(queryPageBean.getQueryString()) == false) {
            queryWrapper.and(wrapper -> {
                wrapper.like("code", queryPageBean.getQueryString()).or()
                        .like("name", queryPageBean.getQueryString()).or()
                        .like("help_code", queryPageBean.getQueryString()).or();
            });
        }
        System.out.println("page = " + page.toString());
        IPage<CheckGroup> groupIPage = this.page(page, queryWrapper);
        return new PageResult(groupIPage.getTotal(),groupIPage.getRecords());
    }

    @Override
    public CheckGroup findById(Long groupId) {
        return this.findById(groupId);
    }

    @Override
    public List<CheckGroupVO> findBySetmealId(Long id) {
        return baseMapper.findBySetmealId(id);
    }


}
